CSS Basics

  • STEPS

    1. CSS Syntax :

    
    Selector {
    		 Property1 : Value1;
    		 Property2 : Value2;
    		 …
    	 }
    
    Example
    
    p {
      color: black;
      background-color: white;
    } 
    

    CSS syntax consists of a selector and a declaration block where Selector is an HTML element which you want to style. Declaration block must be enclosed within { }. Declaration block contains many declaration statements separated by semicolon(;). Each declaration statement contains a CSS property and a value separated by a colon(:).


    2. CSS Selectors :

    CSS selectors are the HTML elements which you want to style. There are different ways to select an HTML element in CSS. Below table describes the same.


    CSS SelectorsExampleExample Description
    Name Selectorp { }Selects all <p> elements.
    ID Selector#myID { }Selects all elements with id=”myID”
    Class Selector.myClass { }Selects the elements with class=”myClass”
    Universal Selector* { }Selects all elements.
    Group Selectorp, div, span { }Selects all <p>, <div> and <span> elements
    Descendant Selectordiv a { }Selects all <a> elements inside <div>
    Child Selectordiv > a { }Selects <a> elements which are direct children of <div>
    General Sibling Selectordiv ~ a { }Selects all <a> elements which are siblings of <div>
    Adjacent Sibling Selectordiv + a { }Selects <a> which come after <div>
    Pseudo Element Selectors::after, ::before, ::first-letter, ::first-line, ::marker, ::selectionThese are used to select and style a part of an element.
    Pseudo Class Selectors:active, :focus, :hover, :link, :visited, :target, :checked, :disabled, :enabled, :empty, :first-child, :last-child, :only-child, :first-of-type, :last-of-type, :invalid, :not(selector), :nth-child(n), :nth-last-child(n), :nth-of-type(n), :nth-last-of-type(n), :in-range, :out-of-range, :required, :optional, :read-only, :read-writeIt selects the elements based on their state.
    Attribute Selectors[attribute],
    [attribute=value], [attribute~=value], [attribute|=value], [attribute^=value], [attribute$=value], [attribute*=value]
    It selects the elements based on attribute and value.

    3. CSS Insertion :

    There are three ways you can link CSS with an HTML document.

    Type Of LinkDescriptionExample
    External CSSLinking external CSS file with an HTML file using <link> tag.<link rel=”stylesheet” href=”style.css”>
    Internal CSSDefining page specific style using <style> element within an HTML page.<style>
    body {color: black;}
    h1 {color: red;}
    </style>
    Inline CSSDefining element specific style using style attribute of an HTML element.<p style=”color:black;”>Inline CSS</p>
  • PROPERTIES

    1. CSS Backgrounds :

    CSS background properties are used to add background effects to an HTML element.

    PropertyValuesDescription
    background-colorcolor | transparent | initial | inheritIt specifies background color of an element.
    background-imageURL | none | initial | inheritIt is used to apply an image as a background to an element.
    background-repeatrepeat | repeat-x | repeat-y | no-repeat | initial | inheritThis property specifies how background image should be repeated.
    background-positionleft top | left center | left bottom | right top | right center | right bottom | center top | center center | center bottom | x% y% | xpos ypos | initial | inheritIt specifies starting position of a background image.
    background-attachmentscroll | fixed | local | initial | inheritThis property specifies whether the background image is fixed or scrolls with the rest of the page.
    background-sizeauto | length | cover | contain | initial | inheritIt specifies the size of the background image.
    background-clipborder-box | padding-box | content-box | initial | inheritIt specifies how far the background color or background image should extend within an element
    background-originpadding-box | border-box | content-box | initial | inheritIt defines origin position of a background image.
    background-blend-modenormal | multiply | screen | overlay | darken | lighten | color-dodge | saturation | color | luminosityIt defines the blending mode of each background layer.
    backgroundbg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial | inheritIt is a shorthand property for all background properties. Using this property, you can specify all background properties in one line.

    2. CSS Borders :

    CSS border properties are used to define style for an element’s border.

    PropertyValuesDescription
    border-styledotted | dashed | solid | double | groove | ridge | inset | outset | hidden | none | inherit | initialIt defines the style for all four borders of an element.
    border-top-styledotted | dashed | solid | double | groove | ridge | inset | outset | hidden | none | inherit | initialIt defines the style for top border of an element.
    border-bottom-styledotted | dashed | solid | double | groove | ridge | inset | outset | hidden | none | inherit | initialIt defines the style for bottom border of an element.
    border-left-styledotted | dashed | solid | double | groove | ridge | inset | outset | hidden | none | inherit | initialIt defines the style for left border of an element.
    border-right-styledotted | dashed | solid | double | groove | ridge | inset | outset | hidden | none | inherit | initialIt defines the style for right border of an element.
    border-widthmedium | thick | thin | length | initial | inheritIt specifies the width of all four borders of an element.
    border-top-widthmedium | thick | thin | length | initial | inheritIt specifies the width of top border of an element.
    border-bottom-widthmedium | thick | thin | length | initial | inheritIt specifies the width of bottom border of an element.
    border-left-widthmedium | thick | thin | length | initial | inheritIt specifies the width of left border of an element.
    border-right-widthmedium | thick | thin | length | initial | inheritIt specifies the width of right border of an element.
    border-colorcolor | transparent | initial | inheritIt sets the color for all four borders of an element.
    border-top-colorcolor | transparent | initial | inheritIt sets the color for top border of an element.
    border-bottom-colorcolor | transparent | initial | inheritIt sets the color for bottom border of an element.
    border-left-colorcolor | transparent | initial | inheritIt sets the color for left border of an element.
    border-right-colorcolor | transparent | initial | inheritIt sets the color for right border of an element.
    border-radiuslength | % | initial | inheritIt applies rounded corners to an element.
    border-top-left-radiuslength | % | initial | inheritIt makes top left corner of an element rounded.
    border-top-right-radiuslength | % | initial | inheritIt makes top right corner of an element rounded.
    border-bottom-left-radiuslength | % | initial | inheritIt makes bottom left corner of an element rounded.
    border-bottom-right-radiuslength | % | initial | inheritIt makes bottom right corner of an element rounded.
    border-imagesource slice width outset repeat | initial | inheritIt specifies an image to be used as a border for an element.
    border-image-sourcenone | URL | initial | inheritIt specifies path to an image to be used as a border of an element.
    border-image-widthlength | % | auto | initial | inheritIt specifies the width of a border image.
    border-image-repeatstretch | repeat | round | space | initial | inheritIt specifies whether the border image of an element should be repeated or stretched or rounded.
    border-image-slicenumber | % | fill | initial | inheritIt specifies how to slice the border image.
    border-image-outsetlength | number | initial | inheritThis property sets outset of a border image.
    borderborder-width border-style border-color | initial | inheritIt is a shorthand property for border-style, border-width and border-color.
    border-bottomborder-width border-style border-color | initial | inheritIt is a shorthand property for border-bottom-style, border-bottom-width and border-bottom-color.
    border-topborder-width border-style border-color | initial | inheritIt is a shorthand property for border-top-style, border-top-width and border-top-color.
    border-leftborder-width border-style border-color | initial | inheritIt is a shorthand property for border-left-style, border-left-width and border-left-color.
    border-rightborder-width border-style border-color | initial | inheritIt is a shorthand property for border-right-style, border-right-width and border-right-color.

    3. CSS Margins :

    CSS margin properties are used to apply space around HTML elements. Margin is the space around an element outside its border.

    PropertyValuesDescription
    marginlength | auto | initial | inheritIt used to set margin of an element around all sides – top, bottom, left and right.
    margin-toplength | auto | initial | inheritIt sets top margin of an element.
    margin-bottomlength | auto | initial | inheritIt sets bottom margin of an element.
    margin-leftlength | auto | initial | inheritIt sets left margin of an element.
    margin-rightlength | auto | initial | inheritIt sets right margin of an element.

    4. CSS Padding :

    CSS padding properties apply space between content of an HTML element and it’s border. Padding is the space around the content of an element inside its border.

    PropertyValuesDescription
    padding-toplength | initial | inheritIt sets top padding of an element.
    padding-bottomlength | initial | inheritIt sets bottom padding of an element.
    padding-leftlength | initial | inheritIt sets left padding of an element.
    padding-rightlength | initial | inheritIt sets right padding of an element.
    paddinglength | initial | inheritIt sets padding at all sides of an element. It is a shorthand property for setting top, bottom, left and right padding in one statement.

    5. CSS Dimensions :

    CSS dimension properties are used to set the height and width of an element.

    PropertyValueDescription
    heightauto | length | % | initial | inheritIt sets height of an element.
    widthauto | length | % | initial | inheritIt sets width of an element.
    max-heightnone | length | % | initial | inheritIt sets maximum height of an element.
    max-widthnone | length | % | initial | inheritIt sets maximum width of an element.
    min-heightlength | % | initial | inheritIt sets minimum height of an element.
    min-widthlength | % | initial | inheritIt sets minimum width of an element.

    6. CSS Shadow :

    These properties are used to add shadow effect to an element.

    PropertyValueDescription
    box-shadownone | h-offset v-offset blur spread color | inset | initial | inheritIt adds shadow effect to an element.
    text-shadowh-shadow v-shadow blur-radius color | none | initial | inheritIt adds shadow effect to the text.

    7. CSS Overflow :

    These properties specify what to do when an element’s content is overflowed.

    PropertyValueDescription
    box-shadownone | h-offset v-offset blur spread color | inset | initial | inheritIt adds shadow effect to an element.
    text-shadowh-shadow v-shadow blur-radius color | none | initial | inheritIt adds shadow effect to the text.

    8. CSS Outline :

    CSS outline properties define outline drawn outside the border of an element.

    PropertyValueDescription
    outline-styledotted | dashed | solid | double | groove | ridge | inset | outset | hidden | none | initial | inheritIt defines style for outline of an element.
    outline-colorinvert | color | initial | inheritIt specifies color for an outline.
    outline-widthmedium | thick | thin | length | initial | inheritIt specifies width of an outline of an element.
    outline-offsetlength | initial | inheritThis property adds space between outline and the border of an element.
    outlineoutline-width outline-style outline-color | initial | inheritIt is a shorthand property for outline-style, outline-color and outline-width.

    9. CSS Text :

    CSS text properties are used to style text of an HTML file.

    PropertyValueDescription
    colorcolor | initial | inheritIt defines the color of a text.
    font-familyfamily-name | generic-family | initial | inheritIt specifies the font family for a text.
    font-sizemedium | xx-small | x-small | small | large | x-large | xx-large | smaller | larger | length | initial | inheritIt specifies font size of a text.
    font-size-adjustnumber | none | initial | inheritIt specifies the font-size adjustment when the selected font-size is not available.
    font-stretchultra-condensed | extra-condensed | condensed | semi-condensed | normal | semi-expanded | expanded | extra-expanded | ultra-expanded | initial | inheritIt makes the text narrower or wider.
    font-stylenormal | italic | oblique | initial | inheritIt specifies font style for a text.
    font-variantnormal | small-caps | initial | inheritIt applies font variation to a text.
    font-weightnormal | bold | bolder | lighter | number | initial | inheritIt specifies font weight of a text.
    fontfont-style font-variant font-weight font-size/line-height font-family | caption | icon | menu | message-box | small-caption | status-bar | initial | inheritIt is a shorthand property for all font related properties.
    text-alignleft | right | center | justify | initial | inheritThis property horizontally aligns the text.
    vertical-alignbaseline | length | sub | super | top | text-top | middle | bottom | text-bottom | initial | inheritIt specifies vertical alignment of not only text but any element.
    text-align-lastauto | left | right | center | justify | start | end | initial | inheritIt specifies horizontal alignment for last line.
    directionltr | rtl | initial | inheritIt sets writing direction of a text.
    text-decoration-linenone | underline | overline | line-through | initial | inheritIt specifies the text decoration like underline, overline or line-thorugh.
    text-decoration-stylesolid | double | dotted | dashed | wavy | initial | inheritIt specifies the style for text decoration line.
    text-decoration-colorcolor | initial | inheritIt specifies the color for text decoration line.
    text-decoration-thicknessauto | from-font | length | percentage | initial | inheritIt specifies the thickness for text decoration line.
    text-decorationtext-decoration-line text-decoration-color text-decoration-style text-decoration-thickness | initial | inheritIt is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style and text-decoration-thickness i.e using this property, you can specify all these properties in a single line declaration.
    text-transformnone | capitalize | uppercase | lowercase | initial | inheritIt defines uppercase or lowercase of a text.
    text-indentlength | initial | inheritIt specifies indentation for first line in a text block.
    line-heightnormal | number | length | initial | inheritIt specifies height of a line.
    letter-spacingnormal | length | initial | inheritIt adds the space between characters of a text.
    word-spacingnormal | length | initial | inheritIt adds the space between words of a text block.
    white-spacenormal | nowrap | pre | pre-line | pre-wrap | initial | inheritIt specifies how to handle the white spaces in a text.
    text-shadowh-shadow v-shadow blur-radius color | none | initial | inheritIt adds the shadow effect to the text.
    word-breaknormal | break-all | keep-all | break-word | initial | inheritIt specifies how words should break at the end of a line.
    word-wrapnormal | break-word | initial | inheritIt specifies how long words to be broken and wrap onto the next line.
    writing-modehorizontal-tb | vertical-rl | vertical-lrIt specifies how lines of text are laid out.
    text-overflowclip | ellipsis | string | initial | inheritIt specifies what to do with overflowed text.
    text-justifyauto | inter-word | inter-character | none | initial | inheritIt specifies how to justify the text.

    10. CSS Lists :

    CSS list properties are used to style ordered and unordered lists.

    PropertyValueDescription
    list-style-typedisc | circle | square | decimal | decimal-leading-zero | georgian | hebrew | hiragana | hiragana-iroha | katakana | katakana-iroha | lower-alpha | lower-greek | lower-latin | lower-roman | upper-alpha | upper-greek | upper-latin | upper-roman | armenian | none | initial | inheritIt specifies the list item marker in a list.
    list-style-imagenone | url | initial | inheritIt specifies an image as a list item marker.
    list-style-positioninside | outside | initial | inheritIt sets the position of a list-item marker.
    list-stylelist-style-type list-style-position list-style-image | initial | inheritIt is a shorthand property for list-style-type, list-style-image and list-style-position.

    11. CSS Table :

    These properties are used to decorate a table.

    PropertyValueDescription
    list-style-typedisc | circle | square | decimal | decimal-leading-zero | georgian | hebrew | hiragana | hiragana-iroha | katakana | katakana-iroha | lower-alpha | lower-greek | lower-latin | lower-roman | upper-alpha | upper-greek | upper-latin | upper-roman | armenian | none | initial | inheritIt specifies the list item marker in a list.
    list-style-imagenone | url | initial | inheritIt specifies an image as a list item marker.
    list-style-positioninside | outside | initial | inheritIt sets the position of a list-item marker.
    list-stylelist-style-type list-style-position list-style-image | initial | inheritIt is a shorthand property for list-style-type, list-style-image and list-style-position.

    12. CSS Visibility :

    These properties control the visibility of an element.

    PropertyValueDescription
    visibilityvisible | hidden | collapse | initial | inheritIt specifies visibility of an element.
    displayinline | block | grid | contents | flex | inline-block | inline-flex | inline-grid | inline-table | list-item | run-in | table | none | initial | inheritIt specifies display behavior of an element.

    13. CSS Positions :

    These properties are used to position an element.

    PropertyValueDescription
    visibilityvisible | hidden | collapse | initial | inheritIt specifies visibility of an element.
    displayinline | block | grid | contents | flex | inline-block | inline-flex | inline-grid | inline-table | list-item | run-in | table | none | initial | inheritIt specifies display behavior of an element.

    14. CSS Columns :

    PropertyValuesDescription
    column-countnumber | auto | initial | inheritIt specifies the number of columns an element should be divided into.
    column-gaplength | normal | initial | inheritIt specifies the gap between the columns.
    column-widthauto | length | initial | inheritIt specifies the width of a column.
    column-spannone | all | initial | inheritIt specifies how many columns a particular element should span across.
    column-fillbalance | auto | initial | inheritIt specifies how to file the columns.
    column-rule-stylenone | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset | initial | inheritIt specifies the style of a rule between the columns.
    column-rule-colorcolor | initial | inheritIt specifies the color for the column rule.
    column-rule-widthmedium | thin | thick | length | initial | inheritIt specifies the width for the column rule.
    column-rulecolumn-rule-width column-rule-style column-rule-color | initial | inheritIt is a shorthand property for column rule properties.
    columnsauto | column-width column-count | initial | inheritIt is a shorthand property for column-width and column-count.

    15. CSS Flexible Elements :

    These properties are related to flexible HTML elements.

    PropertyValueDescription
    ordernumber | initial | inheritIt specifies order for flexible items within the same container.
    flex-basisnumber | auto | initial | inheritIt specifies initial length of a flexible item.
    flex-grownumber | initial | inheritIt specifies how much a flex item can grow within a container.
    flex-shrinknumber | initial | inheritIt specifies how much a flex item can shrink within a container.
    flex-wrapnowrap | wrap | wrap-reverse | initial | inheritIt specifies whether the flexible items should wrap or not.
    flex-directionrow | row-reverse | column | column-reverse | initial | inheritIt specifies the direction for flexible items within a container.
    flex-flowflex-direction flex-wrap | initial | inheritIt is a shorthand property for flex-direction and flex-wrap properties.
    flexflex-grow flex-shrink flex-basis | auto | initial | inheritIt is a shorthand property for flex-grow, flex-shrink and flex-basis.
    align-contentstretch | center | flex-start | flex-end | space-between | space-around | space-evenly | initial | inheritIt is used to align flexible lines inside a flexible container.
    align-itemsstretch | center | flex-start | flex-end | baseline | initial | inheritIt is used to align flexible items inside a flexible container.
    align-selfauto | stretch | center | flex-start | flex-end | baseline | initial | inheritIt is used to align selected item inside a flexible container.
    justify-contentflex-start | flex-end | center | space-between | space-around | space-evenly | initial | inheritIt auto-aligns the flexible items inside a flexible container.

    16. CSS Transitions :

    These properties are used to apply transition effects to an element.

    PropertyValueDescription
    transition-propertynone | all | property | initial | inheritIt specifies the CSS property for which transition effect will takes place.
    transition-durationtime | initial | inheritIt specifies the duration of transition.
    transition-delaytime | initial | inheritIt specifies when the transition effects will start.
    transition-timing-functionlinear | ease | ease-in | ease-out | ease-in-out | step-start | step-end | steps(int,start | end) | cubic-bezier(n,n,n,n) | initial | inheritIt specifies the speed curve of the transition effect.
    transitionproperty duration timing-function delay | initial | inheritIt is a shorthand property for all transition related properties.

    17.CSS 2D / 3D :

    PropertyValueDescription
    transition-propertynone | all | property | initial | inheritIt specifies the CSS property for which transition effect will takes place.
    transition-durationtime | initial | inheritIt specifies the duration of transition.
    transition-delaytime | initial | inheritIt specifies when the transition effects will start.
    transition-timing-functionlinear | ease | ease-in | ease-out | ease-in-out | step-start | step-end | steps(int,start | end) | cubic-bezier(n,n,n,n) | initial | inheritIt specifies the speed curve of the transition effect.
    transitionproperty duration timing-function delay | initial | inheritIt is a shorthand property for all transition related properties.

    18.CSS Animation :

    PropertyValuesDescription
    animation-namekeyframename | none | initial | inheritIt sets the name for animation.
    animation-durationtime | initial | inheritIt specifies time duration of an animation.
    animation-delaytime | initial | inheritThis property specifies time delay for the start of an animation.
    animation-directionnormal | reverse | alternate | alternate-reverse | initial | inheritIt specifies how an animation should play – forward or backward or alternate.
    animation-iteration-countnumber | infinite | initial | inheritIt specifies how many times an animation should be played.
    animation-play-statepaused | running | initial | inheritIt specifies whether an animation is running or paused.
    animation-fill-modenone | forwards | backwards | both | initial | inheritIt specifies the style when an animation is not playing.
    animation-timing-functionlinear | ease | ease-in | ease-out | ease-in-out | step-start | step-end | steps(int,start|end) | cubic-bezier(n,n,n,n) | initial | inheritIt specifies speed curve of an animation.
    animationname duration timing-function delay iteration-count direction fill-mode play-stateIt is a shorthand property for animation properties.

    19. CSS Grid :

    PropertyValueDescription
    row-gaplength | normal | initial | inheritIt specifies the gap between the rows of a grid.
    column-gaplength | normal | initial | inheritIt specifies the gap between the columns of a grid.
    gaprow-gap column-gapIt is a shorthand property for row-gap and column-gap.
    grid-row-startauto | row-lineIt specifies on which row to start displaying grid item.
    grid-row-endauto | row-line | span nIt specifies on which row grid item will end.
    grid-row-gaplengthIt specifies the gap between the rows of a grid layout.
    grid-rowgrid-row-start | grid-row-endIt is a shorthand property for grid-row-start and grid-row-end.
    grid-column-startauto | span n | column-lineIt specifies on which column to start displaying grid items.
    grid-column-endauto | span n | column-lineIt specifies on which column grid items will end.
    grid-column-gaplengthIt specifies the gap between the columns of a grid layout.
    grid-columngrid-column-start / grid-column-endIt is a shorthand property for grid-column-start and grid-column-end.
    grid-gapgrid-row-gap grid-column-gapIt is a shorthand property for grid-row-gap and grid-column-gap.
    grid-areagrid-row-start / grid-column-start / grid-row-end / grid-column-end | itemnameIt is a shorthand property for grid-row-start, grid-column-start, grid-row-end and grid-column-end. You can also use it to name a grid item.
    grid-auto-rowsauto | max-content | min-content | lengthIt sets the default row size of a grid layout.
    grid-auto-columnsauto | max-content | min-content | lengthIt sets default column size.
    grid-auto-flowrow | column | dense | row dense | column denseIt specifies how auto placed items will be inserted in the grid.
    grid-template-rowsnone | auto | max-content | min-content | length | initial | inheritIt specifies height and number of rows in a grid layout.
    grid-template-columnsnone | auto | max-content | min-content | length | initial | inheritIt specifies width and number of columns in a grid layout.
    grid-template-areasnone | itemnamesIt specifies how to display grids using grid names.
    grid-templatenone | grid-template-rows / grid-template-columns | grid-template-areas | initial | inheritIt si a shorthand property for grid-template-rows, grid-template-columns and grid-template-areas properties.
    gridnone | grid-template-rows / grid-template-columns | grid-template-areas | grid-template-rows / [grid-auto-flow] grid-auto-columns | [grid-auto-flow] grid-auto-rows / grid-template-columns | initial | inheritIt is a shorthand property for grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and the grid-auto-flow prpoperties.